package com.xmut.bs.handaccount.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;

import com.dollkey.bottomtabbar.BottomTabBar;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.base.BaseActivity;
import com.xmut.bs.handaccount.fragment.CollectionFragment;
import com.xmut.bs.handaccount.fragment.HomeFragment;
import com.xmut.bs.handaccount.fragment.PersonFragment;
import com.xmut.bs.handaccount.fragment.ShopFragment;
import com.xmut.bs.handaccount.util.PermissionsChecker;

import butterknife.BindView;

public class HomeActivity extends BaseActivity {

    @BindView(R.id.bottom_tab_bar)
    BottomTabBar mBottomTabBar;

    private int currentIndex;
    private String currentName;
    private AlertDialog dialog;
    private static final int REQUEST_CODE_LOCATION = 0x01;

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        PermissionsChecker permissionsChecker = new PermissionsChecker(mContext);
        if (permissionsChecker.lacksPermissions(Manifest.permission.READ_PHONE_STATE
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.CAMERA
                , Manifest.permission.READ_EXTERNAL_STORAGE)) {
            showSimpleDialog(permissionsChecker.getLacksPermissions(Manifest.permission.READ_PHONE_STATE
                    , Manifest.permission.WRITE_EXTERNAL_STORAGE
                    , Manifest.permission.CAMERA
                    , Manifest.permission.READ_EXTERNAL_STORAGE));
        }
        mBottomTabBar.init(getSupportFragmentManager())
                .setDividerColor(colorPrimary)
                .setDividerHeight(1)
                .setChangeColor(colorPrimary, getResources().getColor(R.color.grey500))
                .setIconfont(iconfont)
                .setTabPadding(10, 5, 10)
                .setFontSize(10)
                .setIconFontSize(22)
                .isShowDivider(true)
                .addTabItem(R.string.ic_home, "首页", HomeFragment.class)
                .addTabItem(R.string.ic_discovery, "发现", CollectionFragment.class)
                .addTabItem(R.string.ic_shop, "商店", ShopFragment.class)
                .addTabItem(R.string.ic_my, "我的", PersonFragment.class)
                .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
                    @Override
                    public void onTabChange(int position, String name) {
                        currentIndex = position;
                        currentName = name;
                    }
                });
    }

    @Override
    protected void initParams(Intent intent) {

    }

    @Override
    protected void beforeInit() {
        setSwipeBackEnable(false);
    }

    @Override
    protected boolean isShapeIcon() {
        return true;
    }


    //显示基本Dialog
    private void showSimpleDialog(final String[] strPer) {
        if (dialog != null) {
            dialog.show();
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请允许获取必要权限");
        builder.setMessage("我们需要获取必要权限，否则将无法使用部分功能");
        //监听下方button点击事件
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                ActivityCompat.requestPermissions(HomeActivity.this, strPer, REQUEST_CODE_LOCATION);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                System.exit(0);
            }
        });
        builder.setCancelable(false);
        dialog = builder.create();
        dialog.show();
    }

}
